/*
 * Copyright (c) Huawei Device Co., Ltd. 2024. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef ACCESS_RULE_CACHE_H
#define ACCESS_RULE_CACHE_H
#include <string>
#include <map>
#include "response_all_ref_ar_do.h"
#include "response_refresh_tag_do.h"

namespace OHOS {
namespace SE {
using namespace std;
constexpr const char* FINGER_PRINT_SHA256 = "DF21A3C09F7954579305F85C64F80CAD86F79853EE3A887C1DEC95D218DF3A37";
constexpr const char* FINGER_PRINT_SHA1 = "94A607CBBE0C4E2203ADF094C8D43B8334FB6A10";
enum {
    APDU_ALLOWED = 0,
    NFC_ALLOWED = 1,
    APDU_DENIED = 2,
    NFC_DENIED = 3,
    ACCESS_RULE_END // apdu fileter and permission todo
};

enum {
    EXACT_AID_DEVICEAPP = 0,
    EXACT_AID_ONLY = 1,
    EXACT_DEVICEAPP_ONLY = 2,
    EXACT_NONE = 3,
    EXACT_END
};

enum {
    NO_MATCHED = 0,
    FINGER_MATCHED = 1,
    APPID_MATCHED = 2,
    END_MATCHED,
};

struct StuAccessRules {
    std::map<std::string, std::vector<uint8_t>> mapAidAppId;
    uint32_t accessVal;
};

struct StuAidExactAccessRules {
    std::vector<uint8_t> aid;
    uint32_t accessVal;
};

struct StuAppIdExactAccessRules {
    std::string deviceAppId;
    uint32_t accessVal;
};
class SecureElementAgent;
/*
 * Software that is part of the Secure Element access API, it obtains access
 * rules from the Secure Element and applies those rules to restrict device
 * application access to the various Secure Element applications.
 */
class AccessRuleCache {
public:
    explicit AccessRuleCache(bool isAra, bool isArf, std::weak_ptr<SecureElementAgent> agent);
    ~AccessRuleCache();
    bool Initialize();
    bool IsAllowed(const std::vector<uint8_t> &aid, const std::string &appId, const std::string &fingerPrint);
    bool IsAllowedByFingerPrint(const std::string &fingerPrint);

private:
    bool GetValueByArDo(ArDo &arDoIn, uint32_t &value);
    bool GetStuAccessRulesByRefDo(RefDo &refDoIn, StuAccessRules &retStuAccessVal);
    bool ParseAraResponseAllRefArDo();
    bool ParseArfResponseAllRefArDo();
    bool IsAllowedByAidAndAppId(const std::vector<uint8_t> &aid, const std::string &appId,
        const std::string &fingerPrint, bool &accessRules);
    bool IsAllowedByAidOnly(const std::vector<uint8_t> &aid, bool &accessRules);
    bool IsAllowedByAppIdOnly(const std::string &appId, const std::string &fingerPrint, bool &accessRules);
    bool GetAraResponseAllRefDo();
    bool GetArfArDo(ArDo &inArDo, uint8_t &accessRuleVal);
    bool GetArfExactDeviceAppIdDo(RefDo &inRefDo, struct StuAppIdExactAccessRules &outStuExactAppIdAcRules,
        uint8_t &accessEnumFlags);
    bool GetArfExactAid(RefDo &inRefDo, struct StuAidExactAccessRules &outStuExactAidAcRules,
        uint8_t &accessEnumFlags);
    bool GetArfAidAndAppId(RefDo &inRefDo, struct StuAccessRules &outStuAccessRules,
        uint8_t &accessEnumFlags);
    bool SaveArfAcRules(const uint8_t &accessEnumFlags, uint8_t accessRuleVal,
        struct StuAccessRules &inStuAccessRules, struct StuAidExactAccessRules &inStuExactAidAcRules,
        struct StuAppIdExactAccessRules &inStuExactAppIdAcRules);
    uint32_t GetHmsFingerMap(const std::string &notHmsFingerKey, std::vector<std::string> &hmsFinger);
    bool CompareAllFinger(const std::string &simFinger, const std::string &romFinger);
    bool IsArfAllowedAll(RefDo &inRefDo);

private:
    bool isAra_;
    bool isArf_;
    std::weak_ptr<SecureElementAgent> agent_;
};
}  // namespace SE
}  // namespace OHOS
#endif // ACCESS_RULE_CACHE_H

